package ir.values.users.instructions.mem;

import frontend.visitor.symboltbl.scope.Scope;
import ir.types.datatypes.DataType;
import ir.types.datatypes.PointerType;
import ir.values.Value;
import ir.values.users.instructions.InstOperator;
import ir.values.users.instructions.Instruction;

public class LoadInst extends Instruction {
    private final Value pointerOperand;
    public LoadInst(Value pointerOperand) {
        super(null, ((PointerType) pointerOperand.getType()).getBaseType());
        this.pointerOperand = pointerOperand;
        operands.add(pointerOperand);
    }

    public DataType getDataType() {
        return (DataType) type;
    }

    @Override
    public InstOperator getInstOperator() {
        return InstOperator.load;
    }

    @Override
    public String parseLLVM() {
        // eg: %1 = load i32, i32* %0
        StringBuilder sb = new StringBuilder();
        sb
                .append(parseUsingName())
                .append(" = ")
                .append(getInstOperator())
                .append(" ")
                .append(getDataType().toString())
                .append(", ")
                .append(pointerOperand.getType())
                .append(" ")
                .append(pointerOperand.parseUsingName());
        return sb.toString();
    }
}
